package com.sanjiahantang.youjiaclient.module.me.order.presenters;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

import com.google.gson.Gson;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.uilibrary.views.PayPsdInputView;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseApplication;
import com.sanjiahantang.youjiaclient.base.BaseBean;
import com.sanjiahantang.youjiaclient.base.BaseMvpActivity;
import com.sanjiahantang.youjiaclient.base.BaseMvpPresenter;
import com.sanjiahantang.youjiaclient.base.BaseView;
import com.sanjiahantang.youjiaclient.base.CallBack;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.config.SPConfig;
import com.sanjiahantang.youjiaclient.module.common.beans.StringBean;
import com.sanjiahantang.youjiaclient.module.common.models.IUserModel;
import com.sanjiahantang.youjiaclient.module.common.models.impl.UserModel;
import com.sanjiahantang.youjiaclient.module.common.utils.CommonModelUtils;
import com.sanjiahantang.youjiaclient.module.common.views.impl.ShareSqcodeActivity;
import com.sanjiahantang.youjiaclient.module.me.address.beans.AddressBean;
import com.sanjiahantang.youjiaclient.module.me.address.beans.AddressDetailBean;
import com.sanjiahantang.youjiaclient.module.me.contract.models.IContractModel;
import com.sanjiahantang.youjiaclient.module.me.contract.models.impl.ContractModel;
import com.sanjiahantang.youjiaclient.module.me.order.adapters.OrderAdapter;
import com.sanjiahantang.youjiaclient.module.me.order.adapters.OrderListAdapter;
import com.sanjiahantang.youjiaclient.module.me.order.adapters.ServiceOrderAdapter;
import com.sanjiahantang.youjiaclient.module.me.order.adapters.ShopListOrderAdapter;
import com.sanjiahantang.youjiaclient.module.me.order.beans.CreateOrderPost;
import com.sanjiahantang.youjiaclient.module.me.order.beans.CreateServiceOrderResponseBean;
import com.sanjiahantang.youjiaclient.module.me.order.beans.LogisticsBean;
import com.sanjiahantang.youjiaclient.module.me.order.beans.OrderBean;
import com.sanjiahantang.youjiaclient.module.me.order.beans.OrderResponseBean;
import com.sanjiahantang.youjiaclient.module.me.order.beans.RequireOrderBean;
import com.sanjiahantang.youjiaclient.module.me.order.beans.Service3PartBean;
import com.sanjiahantang.youjiaclient.module.me.order.beans.ServiceListBean;
import com.sanjiahantang.youjiaclient.module.me.order.beans.ShopListBean;
import com.sanjiahantang.youjiaclient.module.me.order.models.IOrderModel;
import com.sanjiahantang.youjiaclient.module.me.order.models.impl.OrderModel;
import com.sanjiahantang.youjiaclient.module.me.order.views.OrderView;
import com.sanjiahantang.youjiaclient.module.me.order.views.impl.OrderActivity;
import com.sanjiahantang.youjiaclient.module.money.beans.BankCardBean;
import com.sanjiahantang.youjiaclient.module.money.views.impl.MeAddBankListActivity;
import com.sanjiahantang.youjiaclient.utils.KeyBoardUtils;
import com.sanjiahantang.youjiaclient.utils.SPUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by sks on 2015/9/29.
 */
public class OrderPresenter extends BaseMvpPresenter<OrderView> {

    private IOrderModel orderModel;
    private IUserModel userModel;
    private OrderListAdapter listAdapter;
    private OrderAdapter orderAdapter;
    private OrderView.OnLoadListener orderDetailOnLoadListener;
    private BaseView.OnLoadListener orderListOnLoadListener;
    private Dialog payDialog, payPasswordDialog;
    private IContractModel contractModel;

    public OrderPresenter() {
        orderModel = new OrderModel();
        userModel = new UserModel();
        contractModel = new ContractModel();
    }


    public void loadOrderList(final Context context, int page, int types, int tag) {
        Map<String, Object> params = new HashMap<>();
        params.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        params.put("phoneTypeId", 0);
        params.put("typeId", tag);
        params.put(KEY_PAGE, 0);
        params.put(KEY_PAGE_SIZE, DEFAULT_PAGE_SIZE);
        params.put(KEY_PHONE, SPUtils.get(context, SPConfig.PHONE, ""));
        mView.showProgress();
        if (types == 1) {
            orderModel.loadOrderList(context, params, new OrderListCallBack());
        } else {
            orderModel.servieceOrdeList(context, params, new ServiceOrderListCallBack());
        }
    }

    public void initRecyclerView(Context context, RecyclerView recyclerView, int type) {


        if (type == OrderView.ACTIVITY_ORDER_CREATE) {
            if (orderAdapter == null) {
                orderAdapter = new OrderAdapter(context, new ArrayList<Integer>());
            }
            recyclerView.setAdapter(orderAdapter);
        }
    }

    public void loadOrder(Context context, String uaId) {
        if (mView == null) return;
        if (uaId == null) {
            Map<String, String> params = new HashMap<>();
            params.put(KEY_TOKEN, ConfigValue.DATA_KEY);
            params.put(KEY_PHONE, SPUtils.get(context, SPConfig.PHONE, "").toString());
            mView.showProgress();
            orderModel.searchUserAddressOrder(context, params, new CallBack<AddressBean>() {
                @Override
                public void onSuccess(AddressBean response, Context context, Object tag) {
                    hideProgress();
                    if (response.getCode() == RESPONSE_CODE_SUC && response.getData().size() > 0) {
                        setAddress(response.getData().get(0));
                    }
                }

                @Override
                public void onFailure(String message) {
                    hideProgress();
                }
            });
        } else {
            Map<String, String> params = new HashMap<>();
            params.put(KEY_TOKEN, ConfigValue.DATA_KEY);
            params.put("ucaId", uaId);
            mView.showProgress();
            userModel.loadAddressDetail(context, params, new CallBack<AddressDetailBean>() {
                @Override
                public void onSuccess(AddressDetailBean response, Context context, Object tag) {
                    hideProgress();
                    if (response.getCode() == RESPONSE_CODE_SUC) {
                        setAddress(response.getData());
                    }
                }

                @Override
                public void onFailure(String message) {
                    hideProgress();
                }
            });
        }

    }

    public void doSubmitOrder(final Context context, RecyclerView recyclerView) {
        final CreateOrderPost post = orderAdapter.getCreateOrderPost();
        if (post == null) {
            return;
        }

        if (post.getUserAddressId() == 0) {
            Utils.showToast(context, "请填写收货地址");
            return;
        }
        mView.showProgress();
        orderModel.createOrderForMallCommon(context, post, new CallBack<OrderResponseBean>() {
            @Override
            public void onSuccess(final OrderResponseBean response, final Context context, Object tag) {
                hideProgress();
                if (response != null && response.getCode() == RESPONSE_CODE_SUC) {
                    //跳支付->回调
                    Intent intent = new Intent(context, OrderActivity.class);
                    intent.putExtra(KEY_TYPE, ORDER_TYPE_PAY);
                    Bundle b = new Bundle();
                    ShopListBean.Data data = new ShopListBean.Data();
                    data.setOrderCode(response.getData().getOrderInfoGoodsRaDtoList().get(0).getOrderCode());
                    b.putSerializable("goods", data);
                    intent.putExtra(KEY_PARAM, b);
                    context.startActivity(intent);
                    ((Activity) context).finish();
                } else {
                    if (response != null) {
                        Utils.showToast(context, response.getMessage());
                    } else {
                        Utils.showToast(context, "网络错误");
                    }
                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
                Utils.showToast(context, message);
            }
        });
    }

    public void setInvoice(String title, String taxNo) {
        orderAdapter.setInvoice(title, taxNo);
    }

    public void setAddress(AddressBean.Data address) {
        orderAdapter.setAddress(address);
    }

    public void setOnLoadListener(OrderView.OnLoadListener onLoadListener) {
        this.orderDetailOnLoadListener = onLoadListener;
    }

    public void doPay(final Context context, final ShopListBean.Data bean) {

        if (payDialog == null) {
            payDialog = new Dialog(context, R.style.Dialog_Fullscreen);
            payDialog.setContentView(R.layout.dialog_pay);
            TextView txtNeedPrice = (TextView) payDialog.findViewById(R.id.txtNeedPrice);

            txtNeedPrice.setText(String.format("%.2f", bean.getPayResult()));
            final RadioButton rbWX = (RadioButton) payDialog.findViewById(R.id.rbWX);
            final RadioButton rbZFB = (RadioButton) payDialog.findViewById(R.id.rbZFB);
            final RadioButton rbBank = (RadioButton) payDialog.findViewById(R.id.rbBank);
            final RadioButton rbBankWeb = (RadioButton) payDialog.findViewById(R.id.rbBankWeb);
            final RadioButton rbPos = (RadioButton) payDialog.findViewById(R.id.rbPos);

            payDialog.findViewById(R.id.btnOK).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    long nowTime = System.currentTimeMillis();
                    if (Math.abs(ConfigValue.nowTime - nowTime) < DOUBLE_CLICK_TIME_SPACE) {
                        return;
                    }

                    ConfigValue.nowTime = nowTime;

                    if (rbWX.isChecked()) {
                        Utils.showToast(context, "很遗憾，微信暂未接入");
                    } else if (rbZFB.isChecked()) {
                        Utils.showToast(context, "很遗憾，支付宝暂未接入");
                    } else if (rbBank.isChecked()) {
                        if (((TextView) payDialog.findViewById(R.id.tvBank)).getText().toString().equals("请先去设置默认银行卡")) {
                            MeAddBankListActivity.fromSettings(context);
                            payDialog.dismiss();
                        } else {
                            payDialog.dismiss();
                            bean.setToken(ConfigValue.DATA_KEY);
                            bean.setPhoneTypeId(0);
                            if (mView == null) return;
                            mView.showProgress();
                            orderModel.getCheckCodeGoods(context, bean, new CallBack<ShopListBean>() {
                                @Override
                                public void onSuccess(ShopListBean response, Context context, Object tag) {
                                    if (mView == null) return;
                                    mView.hideProgress();
                                    if (response != null && response.getCode() == RESPONSE_CODE_SUC) {
                                        if (response.getPayVoBean() != null) {
                                            bean.setPayVo(response.getPayVoBean());
                                            Utils.showToast(context, "验证码已发送");
                                            payPasswordDialog.show();
                                        } else {
                                            Utils.showToast(context, "异常了");
                                        }

                                    } else if (response != null) {
                                        Utils.showToast(context, response.getMessage());
                                    } else {
                                        Utils.showToast(context, "异常了");
                                    }
                                }

                                @Override
                                public void onFailure(String message) {
                                    if (mView == null) return;
                                    mView.hideProgress();
                                    Utils.showToast(context, message);
                                }
                            });
                        }
                    } else if (rbBankWeb.isChecked()) {
                        if (((TextView) payDialog.findViewById(R.id.tvBank)).getText().toString().equals("请先去设置默认银行卡")) {
                            MeAddBankListActivity.fromSettings(context);
                            payDialog.dismiss();
                        } else {
                            payDialog.dismiss();
                            bean.setToken(ConfigValue.DATA_KEY);
                            bean.setPhoneTypeId(0);
                            bean.setIsPos(0);
                            if (mView == null) return;
                            mView.showProgress();
                            orderModel.saveBankPayGoods(context, bean, new CallBack<ShopListBean>() {
                                @Override
                                public void onSuccess(ShopListBean response, Context context, Object tag) {
                                    if (mView == null) return;
                                    mView.hideProgress();
                                    if (response != null) {
                                        Utils.showToast(context, response.getMessage());
                                    } else {
                                        Utils.showToast(context, "异常了");
                                    }
                                }

                                @Override
                                public void onFailure(String message) {
                                    if (mView == null) return;
                                    mView.hideProgress();
                                    Utils.showToast(context, message);
                                }
                            });
                        }
                    } else if (rbPos.isChecked()) {
                        payDialog.dismiss();
                        bean.setToken(ConfigValue.DATA_KEY);
                        bean.setPhoneTypeId(0);
                        bean.setIsPos(1);
                        if (mView == null) return;
                        mView.showProgress();
                        orderModel.saveBankPayGoods(context, bean, new CallBack<ShopListBean>() {
                            @Override
                            public void onSuccess(ShopListBean response, Context context, Object tag) {
                                if (mView == null) return;
                                mView.hideProgress();
                                if (response != null) {
                                    String content = response.getString();
                                    if (!TextUtils.isEmpty(content)) {
                                        content = content.replace("\"", "");
                                        ShareSqcodeActivity.openQRPage(context, content, "POS机付款");
                                    } else {
                                        Utils.showToast(context, "生成POS二维码失败");
                                    }
                                } else {
                                    Utils.showToast(context, "异常了");
                                }
                            }

                            @Override
                            public void onFailure(String message) {
                                if (mView == null) return;
                                mView.hideProgress();
                                Utils.showToast(context, message);
                            }
                        });
                    } else {
                        Utils.showToast(context, "请选择支付方式");
                    }
                }
            });

            payDialog.findViewById(R.id.vWX).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(true);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(false);
                }
            });

            payDialog.findViewById(R.id.vZFB).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(true);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(false);
                }
            });

            payDialog.findViewById(R.id.vBank).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(true);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(false);
                }
            });

            payDialog.findViewById(R.id.vBankWeb).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(true);
                    rbPos.setChecked(false);
                }
            });

            payDialog.findViewById(R.id.vPos).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(true);
                }
            });

            rbWX.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(true);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(false);
                }
            });

            rbZFB.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(true);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(false);
                }
            });

            rbBank.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(true);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(false);
                }
            });

            rbBankWeb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(true);
                    rbPos.setChecked(false);
                }
            });

            rbPos.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(true);
                }
            });
        }


        if (payPasswordDialog == null) {
            payPasswordDialog = new Dialog(context, R.style.dialog_common);
            View payPasswordView = LayoutInflater.from(context).inflate(R.layout.dialog_pay_password, null);
            final PayPsdInputView passwordInputView = (PayPsdInputView) payPasswordView.findViewById(R.id.passwordView);
            payPasswordDialog.setContentView(payPasswordView);
            passwordInputView.setComparePassword(new PayPsdInputView.onPasswordListener() {

                @Override
                public void onDifference(String oldPsd, String newPsd) {

                }

                @Override
                public void onEqual(String psd) {
                }

                @Override
                public void inputFinished(final String inputPsd) {
                    bean.setToken(ConfigValue.DATA_KEY);
                    bean.setPhoneTypeId(0);
                    bean.setPayPwd(inputPsd);
                    ShopListBean.PayVo payVoBean = bean.getPayVo();
                    if (payVoBean != null) {
                        payVoBean.setCheckCode(inputPsd);
                        bean.setPayVo(payVoBean);
                    }
                    passwordInputView.cleanPsd();
                    KeyBoardUtils.closeKeyboardWithoutFinish((Activity) context);
                    mView.showProgress();
                    orderModel.payGoodsOrderPhone(context, bean, new CallBack<BaseBean>() {
                        @Override
                        public void onSuccess(BaseBean response, Context context, Object tag) {
                            payPasswordDialog.dismiss();
                            hideProgress();
                            if (response != null && response.getCode() == RESPONSE_CODE_SUC) {
                                Utils.showToast(context, "支付成功");
                                BaseApplication.clearActivity();
                                ((Activity) context).finish();
                            } else if (response != null) {
                                Utils.showToast(context, response.getMessage());
                            } else {
                                Utils.showToast(context, "很遗憾，支付失败了");
                            }
                        }

                        @Override
                        public void onFailure(String message) {
                            hideProgress();
                            payPasswordDialog.dismiss();
                        }
                    });
                }
            });
        }


        CommonModelUtils.searchBankCardDefault(context, new BaseView.OnLoadListener() {
            @Override
            public void onLoad(Object content) {
                if (content != null && content instanceof BankCardBean.Item) {
                    BankCardBean.Item item = (BankCardBean.Item) content;
                    final TextView tvBank = (TextView) payDialog.findViewById(R.id.tvBank);
                    tvBank.setText(String.format("%s(尾号%s)", item.getBankGenneralName(), Utils.getLast4(item.getBankCardNo())));
                }
                payDialog.show();
//                else {
//                    MeAddBankListActivity.fromSettings(context);
//                    payDialog.dismiss();
//                }
            }
        });

    }

    public void doPayService(final Context context, final ServiceListBean.Data bean) {

        if (payDialog == null) {
            payDialog = new Dialog(context, R.style.Dialog_Fullscreen);
            payDialog.setContentView(R.layout.dialog_pay);
            TextView txtNeedPrice = (TextView) payDialog.findViewById(R.id.txtNeedPrice);
            txtNeedPrice.setText(String.format("%.2f", bean.getPayResult()));
            final RadioButton rbWX = (RadioButton) payDialog.findViewById(R.id.rbWX);
            final RadioButton rbZFB = (RadioButton) payDialog.findViewById(R.id.rbZFB);
            final RadioButton rbBank = (RadioButton) payDialog.findViewById(R.id.rbBank);
            final RadioButton rbBankWeb = (RadioButton) payDialog.findViewById(R.id.rbBankWeb);
            final RadioButton rbPos = (RadioButton) payDialog.findViewById(R.id.rbPos);
            payDialog.findViewById(R.id.btnOK).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    long nowTime = System.currentTimeMillis();
                    if (Math.abs(ConfigValue.nowTime - nowTime) < DOUBLE_CLICK_TIME_SPACE) {
                        return;
                    }
                    ConfigValue.nowTime = nowTime;

                    if (rbWX.isChecked()) {
                        Utils.showToast(context, "很遗憾，微信暂未接入");
                    } else if (rbZFB.isChecked()) {
                        Utils.showToast(context, "很遗憾，支付宝暂未接入");
                    } else if (rbBank.isChecked()) {
                        if (((TextView) payDialog.findViewById(R.id.tvBank)).getText().toString().equals("请先去设置默认银行卡")) {
                            MeAddBankListActivity.fromSettings(context);
                            payDialog.dismiss();
                        } else {
                            payDialog.dismiss();
                            bean.setToken(ConfigValue.DATA_KEY);
                            bean.setPhoneTypeId(0);
                            if (mView == null) return;
                            mView.showProgress();
                            orderModel.getCheckCodeService(context, bean, new CallBack<ShopListBean>() {

                                @Override
                                public void onSuccess(ShopListBean response, Context context, Object tag) {
                                    if (mView == null) return;
                                    mView.hideProgress();
                                    if (response != null && response.getCode() == RESPONSE_CODE_SUC) {
                                        if (response.getPayVoBean() != null) {
                                            bean.setPayVo(response.getPayVoBean());
                                            Utils.showToast(context, "验证码已发送");
                                            payPasswordDialog.show();
                                        } else {
                                            Utils.showToast(context, "异常了");
                                        }

                                    } else if (response != null) {
                                        Utils.showToast(context, response.getMessage());
                                    } else {
                                        Utils.showToast(context, "异常了");
                                    }
                                }

                                @Override
                                public void onFailure(String message) {
                                    if (mView == null) return;
                                    mView.hideProgress();
                                    Utils.showToast(context, "异常了");
                                }
                            });
                        }
                    } else if (rbBankWeb.isChecked()) {
                        if (((TextView) payDialog.findViewById(R.id.tvBank)).getText().toString().equals("请先去设置默认银行卡")) {
                            MeAddBankListActivity.fromSettings(context);
                            payDialog.dismiss();
                        } else {
                            payDialog.dismiss();
                            bean.setToken(ConfigValue.DATA_KEY);
                            bean.setPhoneTypeId(0);
                            bean.setIsPos(0);
                            if (mView == null) return;
                            mView.showProgress();
                            orderModel.saveBankPayService(context, bean, new CallBack<ShopListBean>() {

                                @Override
                                public void onSuccess(ShopListBean response, Context context, Object tag) {
                                    if (mView == null) return;
                                    mView.hideProgress();
                                    if (response != null) {
                                        Utils.showToast(context, response.getMessage());
                                    } else {
                                        Utils.showToast(context, "异常了");
                                    }
                                }

                                @Override
                                public void onFailure(String message) {
                                    if (mView == null) return;
                                    mView.hideProgress();
                                    Utils.showToast(context, "异常了");
                                }
                            });
                        }
                    } else if (rbPos.isChecked()) {
                        payDialog.dismiss();
                        bean.setToken(ConfigValue.DATA_KEY);
                        bean.setPhoneTypeId(0);
                        bean.setIsPos(1);
                        if (mView == null) return;
                        mView.showProgress();
                        orderModel.saveBankPayService(context, bean, new CallBack<ShopListBean>() {
                            @Override
                            public void onSuccess(ShopListBean response, Context context, Object tag) {
                                if (mView == null) return;
                                mView.hideProgress();
                                if (response != null) {
                                    String content = response.getString();
                                    if (!TextUtils.isEmpty(content)) {
                                        content = content.replace("\"", "");
                                        ShareSqcodeActivity.openQRPage(context, content, "POS机付款");
                                    } else {
                                        Utils.showToast(context, "生成POS二维码失败");
                                    }
                                } else {
                                    Utils.showToast(context, "异常了");
                                }
                            }

                            @Override
                            public void onFailure(String message) {
                                if (mView == null) return;
                                mView.hideProgress();
                                Utils.showToast(context, message);
                            }
                        });
                    } else {
                        Utils.showToast(context, "请选择支付方式");
                    }
                }
            });

            payDialog.findViewById(R.id.vWX).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(true);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(false);
                }
            });

            payDialog.findViewById(R.id.vZFB).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(true);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(false);
                }
            });

            payDialog.findViewById(R.id.vBank).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(true);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(false);
                }
            });

            payDialog.findViewById(R.id.vBankWeb).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(true);
                    rbPos.setChecked(false);
                }
            });

            payDialog.findViewById(R.id.vPos).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(true);
                }
            });

            rbWX.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(true);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(false);
                }
            });

            rbZFB.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(true);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(false);
                }
            });

            rbBank.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(true);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(false);
                }
            });

            rbBankWeb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(true);
                    rbPos.setChecked(false);
                }
            });

            rbPos.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(true);
                }
            });
        }

        if (payPasswordDialog == null) {
            payPasswordDialog = new Dialog(context, R.style.dialog_common);
            View payPasswordView = LayoutInflater.from(context).inflate(R.layout.dialog_pay_password, null);
            final PayPsdInputView passwordInputView = (PayPsdInputView) payPasswordView.findViewById(R.id.passwordView);
            payPasswordDialog.setContentView(payPasswordView);
            passwordInputView.setComparePassword(new PayPsdInputView.onPasswordListener() {

                @Override
                public void onDifference(String oldPsd, String newPsd) {

                }

                @Override
                public void onEqual(String psd) {
                }

                @Override
                public void inputFinished(final String inputPsd) {
                    bean.setToken(ConfigValue.DATA_KEY);
                    bean.setPhoneTypeId(0);
                    bean.setPayPwd(inputPsd);
                    ShopListBean.PayVo payVoBean = bean.getPayVo();
                    if (payVoBean != null) {
                        payVoBean.setCheckCode(inputPsd);
                        bean.setPayVo(payVoBean);
                    }
                    passwordInputView.cleanPsd();
                    KeyBoardUtils.closeKeyboardWithoutFinish((Activity) context);
                    if (mView == null) return;
                    mView.showProgress();
                    orderModel.payServiceOrderPhone(context, bean, new CallBack<BaseBean>() {
                        @Override
                        public void onSuccess(BaseBean response, Context context, Object tag) {
                            payPasswordDialog.dismiss();
                            hideProgress();
                            if (response != null && response.getCode() == RESPONSE_CODE_SUC) {
                                Utils.showToast(context, "支付成功");
                                BaseApplication.clearActivity();
                                ((Activity) context).finish();
                            } else if (response != null) {
                                Utils.showToast(context, response.getMessage());
                            } else {
                                Utils.showToast(context, "很遗憾，支付失败了");
                            }
                        }

                        @Override
                        public void onFailure(String message) {
                            hideProgress();
                            payPasswordDialog.dismiss();
                            Utils.showToast(context, "很遗憾，支付失败了");
                        }
                    });
                }
            });
        }

        CommonModelUtils.searchBankCardDefault(context, new BaseView.OnLoadListener() {
            @Override
            public void onLoad(Object content) {
                if (content != null && content instanceof BankCardBean.Item) {
                    BankCardBean.Item item = (BankCardBean.Item) content;
                    final TextView tvBank = (TextView) payDialog.findViewById(R.id.tvBank);
                    tvBank.setText(String.format("%s(尾号%s)", item.getBankGenneralName(), Utils.getLast4(item.getBankCardNo())));
                }
                payDialog.show();
//                else{
//                    MeAddBankListActivity.fromSettings(context);
//                    payDialog.dismiss();
//                }
            }
        });
    }
    public void doPayService(final Context context, final Service3PartBean.Data.OrderServiceRaDtoListBean bean) {

        if (payDialog == null) {
            payDialog = new Dialog(context, R.style.Dialog_Fullscreen);
            payDialog.setContentView(R.layout.dialog_pay);
            TextView txtNeedPrice = (TextView) payDialog.findViewById(R.id.txtNeedPrice);
            txtNeedPrice.setText(String.format("%.2f", bean.getPayResult()));
            final RadioButton rbWX = (RadioButton) payDialog.findViewById(R.id.rbWX);
            final RadioButton rbZFB = (RadioButton) payDialog.findViewById(R.id.rbZFB);
            final RadioButton rbBank = (RadioButton) payDialog.findViewById(R.id.rbBank);
            final RadioButton rbBankWeb = (RadioButton) payDialog.findViewById(R.id.rbBankWeb);
            final RadioButton rbPos = (RadioButton) payDialog.findViewById(R.id.rbPos);
            payDialog.findViewById(R.id.btnOK).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    long nowTime = System.currentTimeMillis();
                    if (Math.abs(ConfigValue.nowTime - nowTime) < DOUBLE_CLICK_TIME_SPACE) {
                        return;
                    }
                    ConfigValue.nowTime = nowTime;

                    if (rbWX.isChecked()) {
                        Utils.showToast(context, "很遗憾，微信暂未接入");
                    } else if (rbZFB.isChecked()) {
                        Utils.showToast(context, "很遗憾，支付宝暂未接入");
                    } else if (rbBank.isChecked()) {
                        if (((TextView) payDialog.findViewById(R.id.tvBank)).getText().toString().equals("请先去设置默认银行卡")) {
                            MeAddBankListActivity.fromSettings(context);
                            payDialog.dismiss();
                        } else {
                            payDialog.dismiss();
                            bean.setToken(ConfigValue.DATA_KEY);
                            bean.setPhoneTypeId(0);
                            if (mView == null) return;
                            mView.showProgress();
                            orderModel.getCheckCodeService(context, bean, new CallBack<ShopListBean>() {

                                @Override
                                public void onSuccess(ShopListBean response, Context context, Object tag) {
                                    if (mView == null) return;
                                    mView.hideProgress();
                                    if (response != null && response.getCode() == RESPONSE_CODE_SUC) {
                                        if (response.getPayVoBean() != null) {
                                            bean.setPayVo(response.getPayVoBean());
                                            Utils.showToast(context, "验证码已发送");
                                            payPasswordDialog.show();
                                        } else {
                                            Utils.showToast(context, "异常了");
                                        }

                                    } else if (response != null) {
                                        Utils.showToast(context, response.getMessage());
                                    } else {
                                        Utils.showToast(context, "异常了");
                                    }
                                }

                                @Override
                                public void onFailure(String message) {
                                    if (mView == null) return;
                                    mView.hideProgress();
                                    Utils.showToast(context, "异常了");
                                }
                            });
                        }
                    } else if (rbBankWeb.isChecked()) {
                        if (((TextView) payDialog.findViewById(R.id.tvBank)).getText().toString().equals("请先去设置默认银行卡")) {
                            MeAddBankListActivity.fromSettings(context);
                            payDialog.dismiss();
                        } else {
                            payDialog.dismiss();
                            bean.setToken(ConfigValue.DATA_KEY);
                            bean.setPhoneTypeId(0);
                            bean.setIsPos(0);
                            if (mView == null) return;
                            mView.showProgress();
                            orderModel.saveBankPayService(context, bean, new CallBack<ShopListBean>() {

                                @Override
                                public void onSuccess(ShopListBean response, Context context, Object tag) {
                                    if (mView == null) return;
                                    mView.hideProgress();
                                    if (response != null) {
                                        Utils.showToast(context, response.getMessage());
                                    } else {
                                        Utils.showToast(context, "异常了");
                                    }
                                }

                                @Override
                                public void onFailure(String message) {
                                    if (mView == null) return;
                                    mView.hideProgress();
                                    Utils.showToast(context, "异常了");
                                }
                            });
                        }
                    } else if (rbPos.isChecked()) {
                        payDialog.dismiss();
                        bean.setToken(ConfigValue.DATA_KEY);
                        bean.setPhoneTypeId(0);
                        bean.setIsPos(1);
                        if (mView == null) return;
                        mView.showProgress();
                        orderModel.saveBankPayService(context, bean, new CallBack<ShopListBean>() {
                            @Override
                            public void onSuccess(ShopListBean response, Context context, Object tag) {
                                if (mView == null) return;
                                mView.hideProgress();
                                if (response != null) {
                                    String content = response.getString();
                                    if (!TextUtils.isEmpty(content)) {
                                        content = content.replace("\"", "");
                                        ShareSqcodeActivity.openQRPage(context, content, "POS机付款");
                                    } else {
                                        Utils.showToast(context, "生成POS二维码失败");
                                    }
                                } else {
                                    Utils.showToast(context, "异常了");
                                }
                            }

                            @Override
                            public void onFailure(String message) {
                                if (mView == null) return;
                                mView.hideProgress();
                                Utils.showToast(context, message);
                            }
                        });
                    } else {
                        Utils.showToast(context, "请选择支付方式");
                    }
                }
            });

            payDialog.findViewById(R.id.vWX).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(true);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(false);
                }
            });

            payDialog.findViewById(R.id.vZFB).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(true);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(false);
                }
            });

            payDialog.findViewById(R.id.vBank).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(true);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(false);
                }
            });

            payDialog.findViewById(R.id.vBankWeb).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(true);
                    rbPos.setChecked(false);
                }
            });

            payDialog.findViewById(R.id.vPos).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(true);
                }
            });

            rbWX.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(true);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(false);
                }
            });

            rbZFB.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(true);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(false);
                }
            });

            rbBank.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(true);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(false);
                }
            });

            rbBankWeb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(true);
                    rbPos.setChecked(false);
                }
            });

            rbPos.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(true);
                }
            });
        }

        if (payPasswordDialog == null) {
            payPasswordDialog = new Dialog(context, R.style.dialog_common);
            View payPasswordView = LayoutInflater.from(context).inflate(R.layout.dialog_pay_password, null);
            final PayPsdInputView passwordInputView = (PayPsdInputView) payPasswordView.findViewById(R.id.passwordView);
            payPasswordDialog.setContentView(payPasswordView);
            passwordInputView.setComparePassword(new PayPsdInputView.onPasswordListener() {

                @Override
                public void onDifference(String oldPsd, String newPsd) {

                }

                @Override
                public void onEqual(String psd) {
                }

                @Override
                public void inputFinished(final String inputPsd) {
                    bean.setToken(ConfigValue.DATA_KEY);
                    bean.setPhoneTypeId(0);
                    bean.setPayPwd(inputPsd);
                    ShopListBean.PayVo payVoBean = bean.getPayVo();
                    if (payVoBean != null) {
                        payVoBean.setCheckCode(inputPsd);
                        bean.setPayVo(payVoBean);
                    }
                    passwordInputView.cleanPsd();
                    KeyBoardUtils.closeKeyboardWithoutFinish((Activity) context);
                    if (mView == null) return;
                    mView.showProgress();
                    orderModel.payServiceOrderPhone(context, bean, new CallBack<BaseBean>() {
                        @Override
                        public void onSuccess(BaseBean response, Context context, Object tag) {
                            payPasswordDialog.dismiss();
                            hideProgress();
                            if (response != null && response.getCode() == RESPONSE_CODE_SUC) {
                                Utils.showToast(context, "支付成功");
                                BaseApplication.clearActivity();
                                ((Activity) context).finish();
                            } else if (response != null) {
                                Utils.showToast(context, response.getMessage());
                            } else {
                                Utils.showToast(context, "很遗憾，支付失败了");
                            }
                        }

                        @Override
                        public void onFailure(String message) {
                            hideProgress();
                            payPasswordDialog.dismiss();
                            Utils.showToast(context, "很遗憾，支付失败了");
                        }
                    });
                }
            });
        }

        CommonModelUtils.searchBankCardDefault(context, new BaseView.OnLoadListener() {
            @Override
            public void onLoad(Object content) {
                if (content != null && content instanceof BankCardBean.Item) {
                    BankCardBean.Item item = (BankCardBean.Item) content;
                    final TextView tvBank = (TextView) payDialog.findViewById(R.id.tvBank);
                    tvBank.setText(String.format("%s(尾号%s)", item.getBankGenneralName(), Utils.getLast4(item.getBankCardNo())));
                }
                payDialog.show();
//                else{
//                    MeAddBankListActivity.fromSettings(context);
//                    payDialog.dismiss();
//                }
            }
        });
    }

    /**
     * /project/phone/plan/searchPriceDesignPhoneForRole
     *
     * @param context
     * @param id
     */
    public void getRequireOrderPrice(Context context, int id, final BaseView.OnLoadListener listener) {
        Map<String, String> param = new HashMap<>();
        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        param.put("requireId", String.valueOf(id));
        mView.showProgress();
        orderModel.searchPriceDesignPhoneForRole(context, param, new CallBack<RequireOrderBean>() {
            @Override
            public void onSuccess(RequireOrderBean response, Context context, Object tag) {
                hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC) {
                    listener.onLoad(response.getData());
                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
            }
        });
    }

    // /order/phone/searchServiceRaForCode

    public void searchServiceRaForCode(Context context, String orderCode, final BaseView.OnLoadListener listener) {
        Map<String, Object> param = new HashMap<>();
        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        param.put("phoneTypeId", 0);
        param.put("orderCode", orderCode);

        mView.showProgress();
        orderModel.searchServiceRaForCode(context, param, new CallBack<Service3PartBean>() {
            @Override
            public void onSuccess(Service3PartBean response, Context context, Object tag) {
                hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC) {
                    if (response.getData() != null
                            && response.getData().getOrderServiceRaDtoList() != null
                            && response.getData().getOrderServiceRaDtoList().size() > 0) {
                        listener.onLoad(response.getData().getOrderServiceRaDtoList().get(0));
                    }

                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
            }
        });
    }

    private void hideProgress() {
        if (mView != null) {
            mView.hideProgress();
        }
    }

    public void setOnLoadListener(BaseView.OnLoadListener listener) {
        this.orderListOnLoadListener = listener;
    }

    public void doCancel(final Context context, final ShopListBean.Data data) {
        Utils.showDialog((Activity) context, "提示", "是否取消订单?", "", "", new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Utils.dismissDialog();
                switch (view.getId()) {
                    case com.sanjiahantang.uilibrary.R.id.txtDialogCancel:

                        break;
                    case com.sanjiahantang.uilibrary.R.id.txtDialogSure:
                        Map<String, Object> param = new HashMap<>();
                        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
                        param.put("phoneTypeId", 0);
                        param.put("secondOrderCode", data.getOrderCode());
                        param.put("typeId", "0");
                        orderModel.cancelOrderPhone(context, param, new CallBack<BaseBean>() {
                            @Override
                            public void onSuccess(BaseBean response, Context context, Object tag) {
                                hideProgress();

                                if (response.getCode() == RESPONSE_CODE_SUC) {
                                    ((Activity) context).finish();
                                }
                                Utils.showToast(context, response.getMessage());
                            }

                            @Override
                            public void onFailure(String message) {
                                hideProgress();
                            }
                        });
                        break;
                }

            }
        });
    }

    public void doCancel(final Context context, final ServiceListBean.Data data) {
        Utils.showDialog((Activity) context, "提示", "是否取消订单?", "", "", new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Utils.dismissDialog();
                switch (view.getId()) {
                    case com.sanjiahantang.uilibrary.R.id.txtDialogCancel:

                        break;
                    case com.sanjiahantang.uilibrary.R.id.txtDialogSure:
                        Map<String, Object> param = new HashMap<>();
                        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
                        param.put("phoneTypeId", 0);
                        param.put("secondOrderCode", data.getOrderCode());
                        param.put("typeId", "1");
                        orderModel.cancelOrderPhone(context, param, new CallBack<BaseBean>() {
                            @Override
                            public void onSuccess(BaseBean response, Context context, Object tag) {
                                hideProgress();
                                if (response.getCode() == RESPONSE_CODE_SUC) {
                                    ((Activity) context).finish();
                                }
                                Utils.showToast(context, response.getMessage());
                            }

                            @Override
                            public void onFailure(String message) {
                                hideProgress();
                            }
                        });
                        break;
                }

            }
        });
    }

    public void createOrderForDesign(Context context, int id, int result, RequireOrderBean.Data data, final BaseView.OnLoadListener listener) {
        Map<String, Object> param = new HashMap<>();
        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        param.put("phoneTypeId", 0);
        param.put("userRequireId", Long.valueOf(String.valueOf(id)));
        param.put("result", result);
        param.put("customerFaPhone", SPUtils.get(context, SPConfig.PHONE, "").toString());
        param.put("skuFaId", data.getSkuFaId());
        param.put("serviceName", data.getServiceName());
        param.put("count", data.getServiceNum());
        param.put("price", data.getServicePrice());
        param.put("ppId", data.getPspId());
        param.put("mgiId", data.getMgiId());
        mView.showProgress();
        orderModel.createOrderForDesign(context, param, new CallBack<CreateServiceOrderResponseBean>() {
            @Override
            public void onSuccess(CreateServiceOrderResponseBean response, Context context, Object tag) {
                hideProgress();
                Utils.showToast(context, response.getMessage());
                if (response.getCode() == RESPONSE_CODE_SUC) {
                    listener.onLoad(response.getData());
                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
            }
        });
    }

    public void operatorState(final Context context, int stateId, String orderRecordCode, final String orderCode, final int isFormPlan) {
        Map<String, Object> param = new HashMap<>();
        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        param.put("typeId", 0);
        param.put("stateId", stateId);
        param.put("phoneTypeId", 0);
        param.put("orderCode", orderRecordCode);
        mView.showProgress();
        orderModel.operatorState(context, param, new CallBack<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response, Context context, Object tag) {
                hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC) {
                    if (isFormPlan == 0) {
                        searchGoodsRaForCode(context, (String) tag, orderListOnLoadListener);
                    } else {
                        searchGoodsRaForCodeForPlan(context, (String) tag, orderListOnLoadListener);
                    }

                } else {
                    Utils.showToast(context, "修改状态失败了");
                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
                searchGoodsRaForCode(context, orderCode, orderListOnLoadListener);
                Utils.showToast(context, "修改状态失败了");
            }
        }, orderCode);
    }

    public void searchGoodsRaForCode(Context context, String orderCode, BaseView.OnLoadListener onLoadListener) {
        if (mView == null) return;
        this.orderListOnLoadListener = onLoadListener;
        Map<String, Object> param = new HashMap<>();
        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        param.put("phoneTypeId", 0);
        param.put("orderCode", orderCode);
        mView.showProgress();
        orderModel.searchGoodsRaForCode(context, param, new CallBack<ShopListBean>() {
            @Override
            public void onSuccess(ShopListBean response, Context context, Object tag) {
                if (mView == null) return;
                hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC) {
                    Object[] para = new Object[2];
                    para[0] = CALLBACK_TYPE_ORDER_DATA;
                    para[1] = response.getItem();
                    orderListOnLoadListener.onLoad(para);
                } else {
                    Object[] para = new Object[2];
                    para[0] = CALLBACK_TYPE_FAILURE;
                    para[1] = "错误";
                    orderListOnLoadListener.onLoad(para);
                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
                Object[] para = new Object[2];
                para[0] = CALLBACK_TYPE_FAILURE;
                para[1] = message;
                orderListOnLoadListener.onLoad(para);
            }
        });
    }

    public void searchGoodsRaForCodeForPlan(Context context, String orderCode, BaseView.OnLoadListener onLoadListener) {
        this.orderListOnLoadListener = onLoadListener;
        Map<String, Object> param = new HashMap<>();
        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        param.put("phoneTypeId", 0);
        param.put("orderCode", orderCode);
        mView.showProgress();
        orderModel.searchGoodsRaForCodeForPlan(context, param, new CallBack<ShopListBean>() {
            @Override
            public void onSuccess(ShopListBean response, Context context, Object tag) {
                hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC) {
                    Object[] para = new Object[2];
                    para[0] = CALLBACK_TYPE_ORDER_DATA;
                    para[1] = response.getItem();
                    orderListOnLoadListener.onLoad(para);
                } else {
                    Object[] para = new Object[2];
                    para[0] = CALLBACK_TYPE_FAILURE;
                    para[1] = "错误";
                    orderListOnLoadListener.onLoad(para);
                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
                Object[] para = new Object[2];
                para[0] = CALLBACK_TYPE_FAILURE;
                para[1] = message;
                orderListOnLoadListener.onLoad(para);
            }
        });
    }

    public void payServiceOrderPhone(Context context, int type, Service3PartBean.Data.OrderServiceRaDtoListBean data) {
        if (type == 1) {
            mView.showProgress();
            orderModel.payServiceOrderPhone(context, data, new CallBack<BaseBean>() {
                @Override
                public void onSuccess(BaseBean response, Context context, Object tag) {
                    hideProgress();
                    if (response != null) {
                        Utils.showToast(context, response.getMessage());
                        if (response.getCode() == RESPONSE_CODE_SUC && context != null) {
                            Utils.showToast(context, "支付成功");
                            ((Activity) context).finish();
                        } else if (response != null) {
                            Utils.showToast(context, response.getMessage());
                        } else {
                            Utils.showToast(context, "很遗憾，支付失败了");
                        }
                    }
                }

                @Override
                public void onFailure(String message) {
                    hideProgress();
                }
            });
        }

    }

    public void saveContractForPhoneDesign(final Context context, Map<String, String> data, final BaseView.OnLoadListener listener) {
        mView.showProgress();
        contractModel.saveContractForPhoneDesign(context, data, new CallBack<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response, Context context, Object tag) {
                if (mView == null) return;
                hideProgress();
                if (response != null && response.getCode() == RESPONSE_CODE_SUC) {
                    listener.onLoad(response);
                } else {
                    Utils.showToast(context, "保存合同失败");
                }

            }

            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                hideProgress();
                Utils.showToast(context, "保存合同失败");
            }
        });
    }

    public void searchServiceOrder(final Context context, String orderCode, final BaseView.OnLoadListener listener) {
        Map<String, String> param = new HashMap<>();
        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        param.put("phoneTypeId", "0");
        param.put("orderCode", orderCode);
        mView.showProgress();
        orderModel.searchServiceOrder(context, param, new CallBack<ServiceListBean>() {
            @Override
            public void onSuccess(ServiceListBean response, Context context, Object tag) {
                if (mView == null) return;
                mView.hideProgress();
                if (response != null && response.getCode() == RESPONSE_CODE_SUC) {
                    listener.onLoad(response.getItem());
                } else if (response != null) {
                    Utils.showToast(context, response.getMessage());
                    listener.onLoad(null);
                } else {
                    Utils.showToast(context, "获取订单信息失败");
                    listener.onLoad(null);
                }
            }

            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                mView.hideProgress();
                Utils.showToast(context, message);
                listener.onLoad(null);
            }
        });
    }

    public void doCancel(final Context context, final Service3PartBean.Data.OrderServiceRaDtoListBean responseData) {
        Utils.showDialog((Activity) context, "提示", "是否取消订单?", "", "", new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Utils.dismissDialog();
                switch (view.getId()) {
                    case com.sanjiahantang.uilibrary.R.id.txtDialogCancel:

                        break;
                    case com.sanjiahantang.uilibrary.R.id.txtDialogSure:
                        Map<String, Object> param = new HashMap<>();
                        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
                        param.put("phoneTypeId", 0);
                        param.put("secondOrderCode", responseData.getOrderCode());
                        param.put("typeId", "1");
                        orderModel.cancelOrderPhone(context, param, new CallBack<BaseBean>() {
                            @Override
                            public void onSuccess(BaseBean response, Context context, Object tag) {
                                hideProgress();
                                if (response.getCode() == RESPONSE_CODE_SUC) {
                                    ((Activity) context).finish();
                                }
                                Utils.showToast(context, response.getMessage());
                            }

                            @Override
                            public void onFailure(String message) {
                                hideProgress();
                            }
                        });
                        break;
                }

            }
        });
    }

    public void findOrderExpressInfo(Context context, String expressCode, String expressNumber, final BaseView.OnLoadListener listener) {
        Map<String, Object> param = new HashMap<>();
        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        param.put("phoneTypeId", 0);
        param.put("expressCode", expressCode);
        param.put("expressNumber", expressNumber);
        orderModel.findOrderExpressInfo(context, param, new CallBack<StringBean>() {
            @Override
            public void onSuccess(StringBean response, Context context, Object tag) {
                if (response != null && response.getCode() == RESPONSE_CODE_SUC) {
                    LogisticsBean bean = new Gson().fromJson("{\"data\":" + response.getData() + "}", LogisticsBean.class);
                    if (bean != null) {
                        LogisticsBean.Data data = bean.getData();
                        listener.onLoad(data);
                    }
                }


            }

            @Override
            public void onFailure(String message) {

            }
        });
    }

    public void getCheckCodeService(final Context context, final Service3PartBean.Data.OrderServiceRaDtoListBean responseData, final BaseView.OnLoadListener listener) {
        orderModel.getCheckCodeService(context, responseData, new CallBack<ShopListBean>() {
            @Override
            public void onSuccess(ShopListBean response, Context context, Object tag) {
                hideProgress();
                if (response != null && response.getCode() == RESPONSE_CODE_SUC) {
                    if (response != null && response.getPayVoBean() != null) {
                        listener.onLoad(response.getPayVoBean());
                    } else {
                        Utils.showToast(context, "异常了");
                    }
                } else if (response != null) {
                    Utils.showToast(context, response.getMessage());
                } else {
                    Utils.showToast(context, "异常了");
                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
                Utils.showToast(context, "异常了");
            }
        });
    }

    public void saveBankPayService(final Context context, final Service3PartBean.Data.OrderServiceRaDtoListBean responseData, final BaseView.OnLoadListener listener) {
        orderModel.saveBankPayService(context, responseData, new CallBack<ShopListBean>() {
            @Override
            public void onSuccess(ShopListBean response, Context context, Object tag) {
                hideProgress();
                if (response != null) {
                    Utils.showToast(context, response.getMessage());
                } else {
                    Utils.showToast(context, "异常了");
                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
                Utils.showToast(context, "异常了");
            }
        });
    }

    public void posPay(final Context context, final Service3PartBean.Data.OrderServiceRaDtoListBean responseData, final BaseView.OnLoadListener listener) {
        orderModel.saveBankPayService(context, responseData, new CallBack<ShopListBean>() {
            @Override
            public void onSuccess(ShopListBean response, Context context, Object tag) {
                hideProgress();
                if (response != null) {
                    String content = response.getString();
                    if (!TextUtils.isEmpty(content)) {
                        content = content.replace("\"", "");
                        ShareSqcodeActivity.openQRPage(context, content, "POS机付款");
                    } else {
                        Utils.showToast(context, "生成POS二维码失败");
                    }
                } else {
                    Utils.showToast(context, "异常了");
                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
                Utils.showToast(context, "异常了");
            }
        });
    }


    //商品订单
    private class OrderListCallBack implements CallBack<ShopListBean> {

        @Override
        public void onSuccess(ShopListBean response, Context context, Object tag) {
            hideProgress();
            if (response != null && response.getCode() == RESPONSE_CODE_SUC) {
                ShopListOrderAdapter shopListOrderAdapter = new ShopListOrderAdapter(context);
                shopListOrderAdapter.setData(response.getData());
                Object[] param = new Object[2];
                param[0] = CALLBACK_TYPE_RECYCLER_ADAPTER;
                param[1] = shopListOrderAdapter;
                orderListOnLoadListener.onLoad(param);
            } else if (response != null && response.getMessage() != null) {
                Utils.showToast(context, response.getMessage());
            }

        }

        @Override
        public void onFailure(String message) {
            hideProgress();
        }
    }

    //服务订单
    private class ServiceOrderListCallBack implements CallBack<ServiceListBean> {

        @Override
        public void onSuccess(ServiceListBean response, Context context, Object tag) {
            hideProgress();
            ServiceOrderAdapter serviceOrderAdapter = new ServiceOrderAdapter(context);
            serviceOrderAdapter.setData(response.getData());

            Object[] param = new Object[2];
            param[0] = CALLBACK_TYPE_RECYCLER_ADAPTER;
            param[1] = serviceOrderAdapter;
            orderListOnLoadListener.onLoad(param);
        }

        @Override
        public void onFailure(String message) {
            hideProgress();
        }
    }


    public void setData(OrderBean.Data data, ArrayList<Integer> typeList, ShopListBean.Data orderInfo, int isFormPlan) {
        orderAdapter.setData(data, typeList, orderInfo, isFormPlan, this);
        orderAdapter.notifyDataSetChanged();
    }
}
